//
//  ChangePhoneNumberViewController.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/9/2.
//
//

#import "ChangePhoneNumberViewController.h"
#import "CheckCodeTableViewCell.h"
#import "CarInfoTableViewCell.h"

@interface ChangePhoneNumberViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CheckCodeTableViewCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
/** 确认更换手机号按钮 */
@property (nonatomic, strong) UIButton *confirmBtn;

@property (nonatomic, strong) UITextField *phoneNumTF;
@property (nonatomic, strong) UITextField *checkCodeTF;
@end

@implementation ChangePhoneNumberViewController
#pragma mark - CheckCodeTableViewCell delegate
- (void)getCheckCode:(UITableViewCell *)cell {
    [self.view showWarning:@"正在获取验证码,请耐心等待!"];
}

#pragma mark - UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.phoneNumTF) {
        [self.checkCodeTF becomeFirstResponder];
    } else if (textField == self.checkCodeTF) {
        [self.checkCodeTF resignFirstResponder];
    }
    return YES;
}

#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CheckCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CheckCodeCell" forIndexPath:indexPath];
        cell.leftInfoLB.text = @"新手机号";
        cell.inputTF.placeholder = @"请输入您的新手机号";
        cell.inputTF.delegate = self;
        self.phoneNumTF = cell.inputTF;
        cell.delegate = self;
        return cell;
    } else {
        CarInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"inputCell" forIndexPath:indexPath];
        [cell.infoTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(kScreenW / 4 + 12 * AUTO_WIDTH);
            make.top.equalTo(4);
            make.bottom.equalTo(-4);
            make.right.equalTo(-8);
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.leftLB.text = @"验证码";
        cell.infoTF.placeholder = @"请输入验证码";
        cell.infoTF.delegate = self;
        self.checkCodeTF = cell.infoTF;
        return cell;
    }
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5 * AUTO_WIDTH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10 * AUTO_WIDTH;
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(0);
            make.bottom.equalTo(self.confirmBtn.mas_top).equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[CheckCodeTableViewCell class] forCellReuseIdentifier:@"CheckCodeCell"];
        [_tableView registerClass:[CarInfoTableViewCell class] forCellReuseIdentifier:@"inputCell"];
    }
    return _tableView;
}

- (UIButton *)confirmBtn {
    if(_confirmBtn == nil) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_confirmBtn];
        [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(0);
            make.height.equalTo(44);
        }];
        [_confirmBtn setTitle:@"确认更换" forState:UIControlStateNormal];
        _confirmBtn.backgroundColor = kRGBA(0, 0, 0, 0.8);
        _confirmBtn.titleLabel.font = defaultFont;
    }
    return _confirmBtn;
}

@end
